package com.ilock.action;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.ilock.bean.Ilocklog;
import com.ilock.bean.Iuser;
import com.ilock.service.ILockLogService;
import com.ilock.service.IUserService;
import com.opensymphony.xwork2.ActionSupport;

public class LeaveWordsAction extends ActionSupport{
	
	private ILockLogService ILockLogService;
	private IUserService IUserService;
	private int lockID;
	private String useremail;
	private String userpassword;
	private String words;
	private Map<String,Object> leavewordsMap = new HashMap<String,Object>();
	
	public ILockLogService getILockLogService() {
		return ILockLogService;
	}
	public void setILockLogService(ILockLogService iLockLogService) {
		ILockLogService = iLockLogService;
	}
	public IUserService getIUserService() {
		return IUserService;
	}
	public void setIUserService(IUserService iUserService) {
		IUserService = iUserService;
	}
	public int getLockID() {
		return lockID;
	}
	public void setLockID(int lockID) {
		this.lockID = lockID;
	}
	public String getUseremail() {
		return useremail;
	}
	public void setUseremail(String useremail) {
		this.useremail = useremail;
	}
	public String getUserpassword() {
		return userpassword;
	}
	public void setUserpassword(String userpassword) {
		this.userpassword = userpassword;
	}
	public String getWords() {
		return words;
	}
	public void setWords(String words) {
		this.words = words;
	}
	public Map<String, Object> getLeavewordsMap() {
		return leavewordsMap;
	}
	public void setLeavewordsMap(Map<String, Object> leavewordsMap) {
		this.leavewordsMap = leavewordsMap;
	}
	
	public String addLeaveWords()throws Exception{
		if(lockID!=0 && useremail!=null && userpassword!=null  && words!=null){
			Iuser user = IUserService.checkIn(useremail, userpassword);
			if(user!=null){
				if(user!=null){
					String leavewords ="msg_"+user.getIuserEmail()+"_"+words;
					Ilocklog ilocklog = new Ilocklog();
					ilocklog.setPriority(-1);
					ilocklog.setSource(2);
					ilocklog.setIlockInfoNumber(lockID);
					System.out.println(leavewords);
					ilocklog.setContent(leavewords);
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					System.out.println(sdf.format(new Date()));
					ilocklog.setDate(Timestamp.valueOf((sdf.format(new Date()))));
					ILockLogService.addLockLog(ilocklog);
					leavewordsMap.put("state", "leavewords:success");
				}else{
					leavewordsMap.put("state", "leavewords:user not existed");
				}
			}else{
				leavewordsMap.put("state", "leavewords:user info error");
			}
		}else{
			leavewordsMap.put("state", "leavewords:info incompleted");
		}
		
		return "success";
	}
	
	

}
